<template>
  <div>
    <el-container class="layout-page">
      <el-aside width="200px">
        <div class="logo">黑马面经</div>
        <!-- route属性就是启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <!-- 然后动态绑定default-active 让这个值等于index的值 -->
        <el-menu
          router
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <el-menu-item index="/dashboard">
            <i class="el-icon-pie-chart"></i>
            <span slot="title">数据看板</span>
          </el-menu-item>

          <el-menu-item index="/article">
            <i class="el-icon-notebook-1"></i>
            <span slot="title">面经管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <el-avatar
            :src="avatar"
          ></el-avatar>
          <!-- 动态绑定underline下划线的布尔值 -->
          <el-link :underline="false">{{ username }}</el-link>
          <!-- 右侧退出的图标 -->
          <div class="logout">
            <template>
              <!--                              查阅官方文档添加了一个点击事件用于点击确认按钮时触发的点击事件 -->
              <el-popconfirm title="您确定退出黑马面经运营后台吗？" @confirm="confirmFn">
                <!-- ↓ 插槽的新式写法 i标签里面添加title就可以让鼠标悬停时显示一个信息 -->
                <template #reference
                  ><i class="el-icon-switch-button" title="logout"></i
                ></template>
              </el-popconfirm>
            </template>
          </div>
        </el-header>
        <el-main>
    <!-- 因为在layout下面写了children二级路由 所以要在对应的组件里面写上二级路由的出口 -->
          <router-view></router-view>
          </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script>
import { getUser } from '@/api/user'
export default {
  name: 'LayoutIndex',
  data () {
    return {
      // console.log($router)
      username: '',
      avatar: ''
    }
  },
  // 想查看$route的话 得页面创建时就打印
  async created () {
    console.log(this.$route)
    const res = await getUser()
    console.log(res)
    this.username = res.data.name
    this.avatar = res.data.avatar
  },
  methods: {
    confirmFn () {
      console.log('确认退出')
      // 清除本地和vuex的token
      this.$store.commit('user/logout')// 去用store里面的user里面的logout方法，因为user被挂在全局 所以可以直接this.
      // 跳到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>
.layout-page {
  height: 100vh; //表示和可视距一样高
  .el-aside {
    background: #313a46;
    .logo {
      color: #fff;
      font-size: 20px;
      height: 60px;
      line-height: 60px;
      text-align: center;
    }
    .el-menu {
      border-right: none;
      margin-top: 20px;
      &-item {
        background-color: transparent !important;
        > span,
        i {
          padding-left: 5px;
        }
      }
    }
  }
  .el-header {
    box-shadow: 0px 0px 35px 0px rgba(154, 161, 171, 0.15);
    background: #fff;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    z-index: 999;
    .user {
      display: flex;
      align-items: center;
      background: #fafbfd;
      height: 60px;
      border: 1px solid #f1f3fa;
      padding: 0 15px;
      .el-avatar {
        margin-right: 15px;
      }
    }
    .logout {
      font-size: 20px;
      color: #999;
      cursor: pointer;
      padding: 0 15px;
    }
  }
}
</style>
